$(document).ready(function(){

    role_access_setup.trigger_actions();

})

role_access_setup = {

    // Edi Selected role access
    trigger_actions : function(){
        $("body").on("click",".trigger_settings",function(){
            var title = $(this).attr("data-original-title");
            var type = $(this).attr("data-type");
            var dataID = $(this).parent().attr("data-id");
            var data_json = $(this).parent().parent().attr("data-json");
            var parse_json = JSON.parse(data_json);
            console.log(parse_json)
            switch (type) {
                
                case "edit":
                    $('[name="access_type[]"]').attr("checked",false);
                    $('[name="access_type[]"]').parent().removeClass("checked");
                    
                    $('[name="page_access"]').val(parse_json['page_access']);
                    $('[name="role_access"]').val(parse_json['role_id']);
                    var access_type = parse_json['access_type'];
                    
                    var split_access_type = access_type.split(",");
                    $.each(split_access_type,function(id,val){
                        //$('[name="access_type[]"]').each(function(aid,aval){
                        //
                        //    if ($(this).val() == val) {
                        //    alert($(this).val() +" == " + val)
                        //        $(this).attr("checked",true);
                        //        $(this).parent().addClass("checked");
                        //    }else{
                                $('[value="' + val + '"]').attr("checked",false);
                                $('[value="' + val + '"]').parent().addClass("checked");
                        //    }
                        //});
                        
                    })
                    $("#access_role").attr("action","/ajax/role-configuration?action=update_role_access&type=update&id=" + dataID);
                    break;
                    
                case "remove":
                    jConfirm( 'Remove Role Access', 'Are you sure do you want to delete this?','', '', '', function(e){
			if (e == true) {
                            $.post("/ajax/role-configuration",{action:type,dataID:dataID},function(data){
                                var data_json = JSON.parse(data);
                                
                                if (data_json['notification'] == "Done.") {
                                
                                    showNotification({
                                        message: data_json['msg'],
                                        type: "success",
                                        autoClose: true,
                                        duration: 3
                                    });
                                    
                                    $("#tr_" + dataID).animate({backgroundColor: "whiteSmoke"}, "fast")
                                        .animate({opacity: "hide"}, "slow");
                                    
                                }
                            });
                        }
                    });
                    break;
            }
        });

    }
    
}

